﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class UIManager : MonoBehaviour {

    private static UIManager _instance;
    public static UIManager _Instance { get { return _instance; } }
    public Transform UIParent;
    private string ResourceDir = "UI";
    //管理里面板的栈
    private Stack<UIBase> UIStack = new Stack<UIBase>();
    //名字 -- 面板的prefab
    private Dictionary<string, GameObject> UIPrefabDic  = new Dictionary<string, GameObject>();
    //已经创建了的面板字典 
    private Dictionary<string, UIBase> UICurrentDic = new Dictionary<string, UIBase>();
    private void Awake()
    {
        LoadAllUIObject();
        _instance = this;
        PushUIPanel("Welcome");
    }
    private void Update()
    {

    }
    /// <summary>
    /// 入栈 显示面板
    /// </summary>
    /// <param name="UIName"></param>
    public void PushUIPanel(string UIName)
    {
        if (UIStack.Count > 0)
        {
            UIBase Old_TopUI = UIStack.Peek();
            Old_TopUI.DoOnPausing();
        }
        //得到面板
        UIBase New_TopUI = GetUIBase(UIName);
        //将得到的新的UI面板入栈
        UIStack.Push(New_TopUI);
        //显示
        New_TopUI.DoOnEntering();
    }
    /// <summary>
    /// 得到UIBase
    /// </summary>
    /// <param name="UIName"></param>
    /// <returns></returns>
    private UIBase GetUIBase(string UIName)
    {
        //如果已经存在已经创建的字典中
        foreach (string name in UICurrentDic.Keys)
        {
            if (name == UIName)
            {
                //UIStack.Push(UICurrentDic[UIName]);//将其再次入栈
                return UICurrentDic[name];                               
            }
        }
        //得到面板prefab
        GameObject UIPrefab = UIPrefabDic[UIName];
        GameObject UIObject = GameObject.Instantiate<GameObject>(UIPrefab);
        //创建面板
        UIObject.transform.SetParent(UIParent,false);        
        UIBase uibase = UIObject.GetComponent<UIBase>();
        //将新创建的UI面板添加到字典中
        UICurrentDic.Add(UIName, uibase);
        //UIStack.Push(uibase);//入栈
        return uibase;
    }
    /// <summary>
    /// 出栈 隐藏面板 
    /// </summary>
    public void PopUIPanel()
    {
        //安全检查 如果已经没有元素就直接返回
        if (UIStack.Count == 0)
            return;
        //取得栈顶的面板 并出栈
        UIBase old_TopUI = UIStack.Pop();
        //执行状态退出
        old_TopUI.DoOnExiting();
        if (UIStack.Count > 0)//如果还有元素
        {
            UIBase new_TopUI = UIStack.Peek(); //取得现在的栈顶面板
            new_TopUI.DoOnResuming();//恢复显示
        }

    }
    /// <summary>
    /// 加载所有的面板并保存在字典中
    /// </summary>
    private void LoadAllUIObject()
    {
        //得到所有的UI
        //用下面的方法预制体的名字必须和所编辑的名字一样
        GameObject[] UIObject = Resources.LoadAll<GameObject>("UI");
        if (UIObject.Length > 0)
        {
            foreach (GameObject go in UIObject)
            {
                //添加到字典
                UIPrefabDic.Add(go.name, go);
            }
        }

        //string path = Application.dataPath + "/" + "Game/Resources" + "/" + ResourceDir;
        //DirectoryInfo foder = new DirectoryInfo(path);
        //foreach (FileInfo file in foder.GetFiles("*.prefab"))
        //{
        //    int index = file.Name.LastIndexOf('.');//得到最后一个.的位置
        //    string UIName = file.Name.Substring(0, index);//得到文件名
        //    GameObject go = Resources.Load<GameObject>(ResourceDir + "/" + UIName);
        //    UIPrefabDic.Add(UIName, go);
        //}

    }
}
